package com.zte.warehouse.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zte.util.PageModel;
import com.zte.warehouse.dao.IInWarehouseDao;
import com.zte.warehouse.dao.IWarehouseDao;
import com.zte.warehouse.model.*;
import com.zte.warehouse.service.IInWarehouseService;
import com.zte.warehouse.service.IWarehouseService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class InWarehouseService implements IInWarehouseService {

    @Autowired
    private IInWarehouseDao iInWarehouseDao;

    //    分页查询
    @Override
    public PageModel<InWarehose> queryInWarehoseListByCondition(PageModel<InWarehose> pageModel, InWarehouseCondition conn) {
        int pageNum = pageModel.getPageNum() <= 0 ? 1 : pageModel.getPageNum();
        int pageSize = pageModel.getPageSize() <= 0 ? 3 : pageModel.getPageSize();

        PageHelper.startPage(pageNum, pageSize);
        List<InWarehose> inWarehoses = iInWarehouseDao.queryInWarehoseListByCondition(conn);
        PageInfo<InWarehose> info = new PageInfo<InWarehose>(inWarehoses);

        pageModel.setPageNum(info.getPageNum());
        pageModel.setPageSize(info.getPageSize());
        pageModel.setTotalPage(info.getPages());
        pageModel.setTotalCount((int) info.getTotal());
        pageModel.setData(new ArrayList<InWarehose>(info.getList()));
        return pageModel;
    }

    //    添加入库
    @Override
    public int addInWarehose(AddInWarehose addInWarehose) {
        //更新
        Purchase purchase = new Purchase();
        purchase.setPurchaseId(addInWarehose.getIndent());
        purchase.setCgState(2);
        iInWarehouseDao.updatePurchase(purchase);//更新采购单入库状态，避免重复入库
        return iInWarehouseDao.addInWarehose(addInWarehose);
    }

    @Override
    public Purchase testFunction(InWarehouseUpdateCondition con) {
        Purchase purchase = iInWarehouseDao.queryPurchaseAndDetails(con.getPurchaseId());

        return purchase;
    }

    //    商品入库
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
    public int updateInWarehose(InWarehouseUpdateCondition con) {
        //1.更新仓库账面金额

        //2.更新入库表入库状态

        //3.查询采购单、采购详情

        //4.遍历确定该类商品库存状态， 是否创建库存/是否删除库存， 并更新数量

        //有则，更新库存数量  WarehouseID,productId  count

        //无则，创建新的商品库存，更新数量  查出要插入库存数据  purchaseId，AddInWarehose

        //出入库操作  一采购单——多种采购商品
        String state = con.getState();
        System.out.println(state+":State");
        System.out.println("con:" + con);

        //更新入库表入库状态  purchaseId/id    state 2/1
        AddInWarehose addInWarehose = new AddInWarehose();
        if (state.equals("1")) {
            addInWarehose.setState("2");
        } else {
            addInWarehose.setState("1");
        }
        addInWarehose.setIndent(con.getPurchaseId());
        int i1 = iInWarehouseDao.updateInWarehose(addInWarehose);
        System.out.println("addInWarehose:" + addInWarehose);
        System.out.println("i1:" + i1);

        //更新仓库账面金额   WarehouseID  lastWmoney
        SingleWarehouse singleWarehouse = new SingleWarehouse();
        singleWarehouse.setId(con.getWarehouseID());
        singleWarehouse.setWMoney(con.getLastWmoney());
        int i2 = iInWarehouseDao.updateWarehose(singleWarehouse);
        System.out.println("singleWarehouse:" + singleWarehouse);
        System.out.println("i2:" + i2);

        //!查询采购单、采购详情   purchaseId  warehuoseId  count（据purchaseId获取）
        Purchase purchase = iInWarehouseDao.queryPurchaseAndDetails(con.getPurchaseId());
        System.out.println();
        List<PurchaseDetails> purchaseDatails = purchase.getPurchaseDatails();//所有采购详情
        System.out.println("purchaseDatails:" + purchaseDatails);
        List<StockInsert> stockInserts = iInWarehouseDao.queryInsertStockMessage(con.getPurchaseId());//所有采购详情对应库存数据  //查询出要插入库存的库存信息  purchaseId
        System.out.println("stockInserts:" + stockInserts);

        //!查询库中是否有存该商品   WarehouseID,productId      ，  count
        StockInsert stockInsert = new StockInsert();
        stockInsert.setWarehouseId(con.getWarehouseID());//更新库存对象
        int result=0;
        for (int i = 0; i < purchaseDatails.size(); i++) {
            stockInsert.setProductId(purchaseDatails.get(i).getProductId());
            StockInsert selectStock = iInWarehouseDao.queryIsStock(stockInsert);//该种商品已有库存
            if (state.equals("1")) {//入库
                if (selectStock != null) {//对应仓库中已存改类商品
                    stockInsert.setRepertory(purchaseDatails.get(i).getCount() + selectStock.getRepertory());//库存商品数量增加
                    result=iInWarehouseDao.updateIsStock(stockInsert);  //!更新库存商品数量   WarehouseID,productId      ，  count
                    System.out.println(stockInsert.getWarehouseId()+"号仓库添加"+stockInsert.getProductId()+"号商品至"+stockInsert.getRepertory()+"件");
                } else {
                    StockInsert newstock = stockInserts.get(i);
                    newstock.setRepertory(purchaseDatails.get(i).getCount());
                    result= iInWarehouseDao.InsertStockMessage(newstock); //库存未存该商品，插入库存商品
                    System.out.println(newstock.getWarehouseId()+"号仓库新创建"+newstock.getProductId()+"号商品"+newstock.getRepertory()+"件");
                }
            } else {//取消入库
                    stockInsert.setRepertory(selectStock.getRepertory() - purchaseDatails.get(i).getCount());//库存商品数量减少
                    result = iInWarehouseDao.updateIsStock(stockInsert);  //!更新库存商品数量   WarehouseID,productId      ，  count
                    System.out.println(stockInsert.getWarehouseId() + "号仓库减少" + stockInsert.getProductId() + "号商品至" + stockInsert.getRepertory() + "件");
            }
            System.out.println("selectStock:"+selectStock);
            System.out.println("stockInsert:"+stockInsert);
        }
            return result;
        }

        //    验证采购单审核状态
        @Override
        public Purchase queryPurchaseState (String indent){
            return iInWarehouseDao.queryPurchaseState(indent);
        }
    }
